package org.dizem.freepl0.gui;

import org.dizem.freepl0.gui.controller.FPController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by IntelliJ IDEA.
 * User: DIZEM
 * Date: 11-2-2
 * Time: 下午3:32
 */

public class MainFrame extends JFrame {
	private FPController controller;

	public MainFrame(FPController controller) throws HeadlessException {
		this.controller = controller;

		initWindow();
		add(controller.getToolBar(), BorderLayout.PAGE_START);
		add(makePane(controller), BorderLayout.CENTER);
		setVisible(true);
		controller.loadFile();
	}

	private JSplitPane makePane(FPController controller) {

		JTabbedPane southWestPane = new JTabbedPane();
		southWestPane.add(new JScrollPane(controller.getOutputPanel()), "Output");
		JTabbedPane southEastPane = new JTabbedPane();
		southEastPane.add(new JScrollPane(controller.getTargetCodePanel()), "Object Code");
		JSplitPane southPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, southWestPane, southEastPane);
		southPane.setDividerLocation(300);
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, controller.getTabPanel(), southPane);
		splitPane.setDividerLocation(250);
		return splitPane;
	}

	private void initWindow() {
		setTitle("FreePL0 Lite");
		setLayout(new BorderLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(600, 500);
		setLocation((screenSize.width - 600) / 2, (screenSize.height - 500) / 2);

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				controller.exit();
			}
		});
	}
}
